
/*
游戏存档数据
 */
import DataStorage from "./DataStorage";


export default class GameData
{
    public dataVersion:number=1;
    //当前关卡ID
    public curLevelId:number=0;

    public money:number=300;
    public star:number=0;
    public hp:number=10;

    //音效
    public snd:string="on";

    //音乐
    public music:string="on";

    //震动
    public vibration:string="on";

    //是否显示过协议
    public agreed:string="no";

    public cityStar:number=0;
    public city:string="未定位";

    public hpResumeTime:number=0;

    public static dataName:string="sfgameballxiaoxiao2023";

    //重置游戏数据
    public reset()
    {
        this.dataVersion=1;

        this.curLevelId=0;
        this.money=300;
        this.star=0;
        this.cityStar=0;
        this.hp=10;
        this.snd="on";
        this.music="on";
        this.vibration="on";
        this.agreed="no";
        this.city="未定位";
        this.hpResumeTime=0;
    }

    public isSndOn():boolean
    {
        if(this.snd==="on")
            return true;
        return false;
    }

    public isMusicOn():boolean
    {
        if(this.music==="on")
            return true;
        return false;
    }

    public isVibrationOn():boolean
    {
        if(this.vibration==="on")
            return true;
        return false;
    }

    //是否已经同意协议
    public isAgreed():boolean
    {
        if(DataStorage.dataExist(GameData.dataName)===false)
            return false;

        if(this.agreed==="yes")
            return true;
        return false;
    }

    public updateAgreed(agreed:boolean)
    {
        if(agreed)
            this.agreed="yes";
        else
            this.agreed="no";
        this.save();
    }

    public load()
    {
        if(DataStorage.dataExist(GameData.dataName))
        {
            let data=DataStorage.load(GameData.dataName);
            this.dataVersion=data.dataVersion;
            this.curLevelId=data.curLevelId;
            this.money=data.money;
            this.star=data.star;
            this.hp=data.hp;
            this.snd=data.snd;
            this.music=data.music;
            this.vibration=data.vibration;
            this.agreed=data.agreed;
            this.city=data.city;
            if(!this.city)
                this.city="未定位";

            this.cityStar=data.cityStar;
            if(!this.cityStar)
                this.cityStar=0;

            this.hpResumeTime=data.hpResumeTime;
            if(!this.hpResumeTime)
                this.hpResumeTime=0;
        }
        else
        {
            this.reset();
            DataStorage.save(GameData.dataName, this);
        }
    }

    public save()
    {
        DataStorage.save(GameData.dataName, this);
    }

    public clear()
    {
        DataStorage.delete(GameData.dataName);
    }

    public updateVibration(on:boolean)
    {
        if(on)
        {
            this.vibration="on";
        }
        else
        {
            this.vibration="off";
        }
        this.save();
    }

    public setHpResumeTime()
    {
        this.hpResumeTime=Date.now();
        this.save();
    }

    public resetHpResumeTime()
    {
        this.hpResumeTime=0;
        this.save();
    }

    public updateCity(city:string)
    {
        this.city=city;
        this.save();
    }

    public updateSnd(on:boolean)
    {
        if(on)
        {
            this.snd="on";
        }
        else
        {
            this.snd="off";
        }
        this.save();
    }

    public updateMusic(on:boolean)
    {
        if(on)
        {
            this.music="on";
        }
        else
        {
            this.music="off";
        }
        this.save();
    }

    public updateMoney(val:number):boolean
    {
        if(val<0)
            return false;
        this.money=val;
        this.save();
        return true;
    }

    public addMoney(val:number)
    {
        this.money+=val;
        if(this.money<0)
            this.money=0;
        this.save();
    }

    public addStar(val:number)
    {
        this.star+=val;
        if(this.star<0)
            this.star=0;
        this.save();
    }

    public addCityStar(val:number)
    {
        this.cityStar+=val;
        this.save();
    }

    public addHp(val:number)
    {
        this.hp+=val;
        if(this.hp<0)
            this.hp=0;
        this.save();
    }

    public nextLevel()
    {
        this.curLevelId++;
        this.save();
    }
}


















